<template>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
      <!-- Content Header (Page header) -->
      <section class="content-header">
        <h1>
          客户管理
        </h1>

        <ol class="breadcrumb">
          <li><a href="#"> 控制台</a></li>
          <li><a href="#">联系人信息</a></li>
          <li><a href="#">拜访记录</a></li>
          <li class="active">编辑</li>
        </ol>
      </section>

      <!-- Main content -->
      <section class="content">
          <div class="row">
            <div class="col-xs-12">
                <div class="box box-solid">
                  <div class="box-header with-border">
                    <h3 class="box-title">拜访记录编辑</h3>
                  </div>
                  <!-- /.box-header -->
                  <!-- form start -->
                  <form class="form-horizontal">
                    <div class="box-body">
                        <div class="row">
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('visit_date')}]">
                                <label class="col-sm-2 control-label">时间</label>
                                <div class="col-sm-10">
                                  <datepicker v-model="model.visit_date" :not-before="new Date()" placeholder="点击选择时间" input-class="form-control col-sm-6" :width="300"></datepicker>
                                  <span class="help-block" v-if="errors.has('visit_date')">{{errors.first('visit_date')}}</span>
                                </div>
                            </div>
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('address')}]">
                                <label  class="col-sm-2 control-label">地点</label>
                                <div class="col-sm-10">
                                  <input class="form-control" name="address" v-validate="'required'" v-model="model.address">
                                  <span class="help-block" v-if="errors.has('address')">{{errors.first('address')}}</span>
                                </div>
                            </div>
                        </div>


                        <div class="row">
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('contact_name')}]">
                                <label  class="col-sm-2 control-label">联系人</label>
                                <div class="col-sm-10">
                                    <input class="form-control" placeholder="" name="contact_name" v-validate="'required'" v-model="model.contact_name">
                                    <span class="help-block" v-if="errors.has('contact_name')">{{errors.first('contact_name')}}</span>
                                </div>
                            </div>
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('intention')}]">
                                <label  class="col-sm-2 control-label">目的</label>
                                <div class="col-sm-10">
                                    <input class="form-control" placeholder="" name="intention" v-validate="'required'" v-model="model.intention">
                                    <span class="help-block" v-if="errors.has('intention')">{{errors.first('intention')}}</span>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('remark')}]">
                                <label class="col-sm-2 control-label">备注</label>
                                <div class="col-sm-10">
                                  <input class="form-control" placeholder="" name="remark" v-model="model.remark">
                                  <span class="help-block" v-if="errors.has('remark')">{{errors.first('remark')}}</span>
                                </div>
                            </div>
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('result')}]">
                                <label  class="col-sm-2 control-label">结果</label>
                                <div class="col-sm-10">
                                  <input class="form-control" placeholder="" name="result" v-model="model.result">
                                  <span class="help-block" v-if="errors.has('result')">{{errors.first('result')}}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                      <button type="button" class="btn btn-primary" @click="submit">保存</button>
                    </div>
                    <!-- /.box-footer -->
                  </form>
                </div>
              <!-- /.box -->
            </div>
          </div>
      </section>
      <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
</template>

<script>
import Visit from '@/modules/Visit'
import DatePicker from 'vue2-datepicker'

export default {

    mounted() {
        this.loadModel();
    },

    data() {
        return {
            model:{
                visit_date:new Date()
            },
        };
    },

    methods: {
        loadModel() {
            if(this.$route.params.id > 0){
                Visit.show(this.$route.params.id)
                .then((response) => {
                    this.model = response.contents;
                    var fullDate = response.contents.visit_date.split("-");
                    this.model.visit_date =  new Date(fullDate[0], fullDate[1]-1, fullDate[2], 0, 0, 0);
                });
            }
        },

        submit() {
            this.$validator.validateAll().then((result) => {
                if(!result){
                    return;
                }
                var params = {
                    ...this.model
                };
                params.visit_date = this.model.visit_date.getFullYear()+'-'+this.model.visit_date.getMonth()+1+'-'+this.model.visit_date.getDate();
                if(this.$route.params.id > 0){
                    Visit.put(this.$route.params.id,params)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'VisitIndex'});
                    })
                }else{
                    Visit.store(params)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'VisitIndex'});
                    });
                }
            });
        }
    },

    computed:{

    },

    components:{
        'datepicker':DatePicker
    }
}
</script>
